<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BoxCare extends Model
{
    public $table = 'box_care';

    protected $fillable = [
    	'id',
        'number',
        'status',
        'send_address',
        'order_id',
    ];

    public function order()
    {
        return $this->hasMany(Order::class,'id','order_id');
    }


    // public  function BoxSend()
    // {
    //     return $this->hasOne('App\Models\BoxSend','box_id','id');
    // }


    public  function collocation()
    {
        return $this->hasOne('App\Models\Collocation','box_id','id');
    }

    public  function user()
    {
        return $this->hasOne('App\Models\UsersWechat','id','wechat_id');
    }

    public  function getStatusName(){
        $status = $this->getAttributeValue('status');
        switch ($status) {
            case 1:
                return '待搭配';
                break;
            case 2:
                return '搭配中';
                break;
            case 3:
                return '待发货';
                break;
            case 4:
                return '待收货';
                break;
            case 5:
                return '待评论';
                break;
            case 6:
                return '完成';
                break;
            default:
                return '未知状态';
                break;
        }
    }

    public static function addBoxCare($wechatId){
        $user = UsersWechat::find($wechatId);
        for( $i= 0;$i<4;$i++){
            $n  = BoxCare::where('wechat_id',$wechatId)->count('id');
            $n = intval($n)+1;
            $box = new BoxCare();
            $box->wechat_id= $wechatId;
            $box->number= $user->number.'-'.$n.'-'.date('YmdHis');
            // if ($i > 1) {
            //     $box->is_frozen= 0;
            // }else{
            //     $box->is_frozen= 1;
            // }
            $box->is_frozen= 1;
            $box->status = 1;
            $box->save();
        }
        return true;
    }
}
